var Identity = Vue.component('identity', {
    data() {
        return {
            editStatus: true,      // 是否可编辑
            userInfo: {},
            legal_licences: {},
            com_licences: {},
            legal_licences_old: {},
            com_licences_old: {}
        }
    },

    template: `<div class="identity__box">
                    <div class="identity__con">
                        <h3 class="index__title identity__title">法人证件</h3>
                        <div class="index__info identity__info">
                            <div class="identity__info__list">
                                <span>形象照：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="legal_licences.showXxz">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="showXxz" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>身份证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="legal_licences.showSfz">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="showSfz" @change="uploadLegal">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>毕业证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="legal_licences.showByz">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="showByz" @change="uploadLegal">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>奖状：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="legal_licences.showJz">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="showJz" @change="uploadLegal">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>结婚证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="legal_licences.showJhz">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="showJhz" @change="uploadLegal">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>驾驶证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="legal_licences.showJsz">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="showJsz" @change="uploadLegal">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <h3 class="index__title identity__title">公司证件</h3>
                        <div class="index__info identity__info">
                            <div class="identity__info__list">
                                <span>组织机构代码：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.zzjgdmzs">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="zzjgdmzs" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>营业执照：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.yyzzs">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="yyzzs" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>税务登记证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.swdjzs">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="swdjzs" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>租聘合同：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.rents">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="rents" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>各类许可证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.licencess">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="licencess" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>荣誉证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.honorss">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="honorss" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                            <div class="identity__info__list">
                                <span>房产证：</span>
                                <div class="identity__img__list">
                                    <div class="identity__img">
                                        <img :src="com_licences.houses">
                                    </div>
                                    <div class="identity__img__btn" v-show="!editStatus" title="替换图片">
                                        <input type="file" data-name="houses" @change="uploadCom">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="common__btn">
                            <div class="common__btn__box">
                                <span class="common__btn__sure" v-show="editStatus" @click="setEditFun">编辑</span>
                                <span class="common__btn__close" v-show="!editStatus" @click="setCancelFun">取消</span>
                                <span class="common__btn__sure" v-show="!editStatus" @click="updateUserInfo">保存设置</span>
                            </div>
                        </div>
                    </div>
                </div>`,

    methods: {
        setEditFun: function () {  // 编辑
            this.editStatus = !this.editStatus
        },
        setCancelFun: function () { // 取消
            console.log(this.com_licences_old)
            console.log(this.legal_licences_old)
            var com_licences_old = JSON.stringify(this.com_licences_old);
            this.com_licences = JSON.parse(com_licences_old);

            var legal_licences_old = JSON.stringify(this.legal_licences_old);
            this.legal_licences = JSON.parse(legal_licences_old);
            this.editStatus = !this.editStatus;
        },
        getUserInfo() {
            // 获取用户信息
            var self = this;
            $.ajax({
                type:"POST",
                url: _UrlPrefix + "/api/member/company/filelist",
                data: {
                    app_access: _AppAccess,
                    token: localStorage.getItem('token'),
                    type: 'gs',
                    cate: 2
                },
                success:function(res){
                    if(0 == res.code) {
                        var data = res.data;
                        var _userInfo = JSON.stringify(data);
                        self.userInfo = JSON.parse(_userInfo);
                    
                        if(!data.legal_licences || data.legal_licences == "null") {
                            self.legal_licences = {};
                            self.legal_licences_old = {};
                        }else {
                            self.legal_licences = JSON.parse(data.legal_licences);
                            self.legal_licences_old = JSON.parse(data.legal_licences);
                        }
                        if(!data.com_licences || data.com_licences == "null") {
                            self.com_licences = {};
                            self.com_licences_old = {};
                        }else {
                            self.com_licences = JSON.parse(data.com_licences);
                            self.com_licences_old = JSON.parse(data.com_licences);
                        }
                    }
                }
            })
        },
        uploadLegal(e) {
            // 上传法人证件
            var _target = e.target;
            var _name = _target.dataset.name;
            var _files = _target.files[0];

            onUploadImg(_files, (res) => {
                if(res.code == 0) {
                    this.legal_licences[_name] = res.data.img;
                }
            })
        },
        uploadCom(e) {
            // 上传公司证件
            var _target = e.target;
            var _name = _target.dataset.name;
            var _files = _target.files[0];
            onUploadImg(_files, (res) => {
                if(res.code == 0) {
                    this.com_licences[_name] = res.data.img;
                }
            })
        },
        updateUserInfo() {
            // 修改用户信息
            var self = this;
            $.ajax({
                type:"POST",
                url: _UrlPrefix + "/api/member/factory/file-upload",
                data: {
                    app_access: _AppAccess,
                    token: localStorage.getItem('token'),
                    type: "gs",
                    legal_licences: JSON.stringify(this.legal_licences),
                    com_licences: JSON.stringify(this.com_licences)
                },
                success:function(res){
                    console.log(res)
                    if(0 == res.code) {
                        ELEMENT.Message.success('保存成功');
                        self.editStatus = !self.editStatus;
                    }
                }
            })
        }
    },
    mounted() {
        
    },
    created() {
        // 获取用户信息
        this.getUserInfo();
    },
})